<?php
// +----------------------------------------------------------------------
// | 随访系统
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2050 http://www.hedesoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------
// | Author: 小李子 < 490959289@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Cache;
use think\Session;
use think\Db;
class Base extends Controller
{
    /*
    *控制器初始化
    *登录状态检查
    *权限检查
    *控制台菜单
    */
    public function _initialize()
    {
        $this->request = $request = Request::instance();
        $this->admin_user = Session::get('user');
        $admin_login = Session::get('admin_login');
        if ($request->isAjax()==false && empty($admin_login)){
            //不是ajax 且 登录失效
            //$this->error('登录已失效，请重新登录',url('admin/Login/index'));
            header('location:'.url('admin/Login/index'));
        }elseif ($request->isAjax()==true && empty($admin_login)){
            //是Ajax 且 登录失效
            echo json_encode(array('status'=>0,'msg'=>'登录已失效，请重新登录'));exit();
        }
        $url_act = [
            'module'=>$request->module(),
            'controller'=>$request->controller(),
            'action'=>$request->action(),
        ];
        $limit = $this->check_limits($url_act);
       
        if ($request->isAjax()==false && empty($limit['status'])){
            //不是ajax 且 无权限
            $this->error('您无权限对此进行操作');
        }elseif ($request->isAjax()==true && empty($limit['status'])){
            //是Ajax 且 无权限
            echo json_encode(array('status'=>0,'msg'=>'您无权限对此进行操作'));exit();
        }
        $this->assign('sys_config',$this->getConfig());
        $this->assign('admin_menu',$this->admin_menu());
        $this->assign('url_act',$url_act);
        $this->assign('menu_sign',$this->now_menu($url_act));
        $this->assign('adminUser',$this->admin_user);
    }
    /**
     * 控制台菜单
     * @param Request $request Request对象
     * @access public
     */
    public function admin_menu()
    {
        $menu_list=Cache::get('admin_menu');
        if(empty($menu_list)){
            $db = Db::name('admin_menu');
            //todo 暂时不创建模型
            $menu_list = $db->where('parent_id',0)->where('menu_status',1)->order('menu_sort','asc')->select();
            foreach ($menu_list as $key => $val) {
                $menu_list[$key]['menu'] = $db->where('parent_id',$val['menu_id'])->where('menu_status',1)->order('menu_sort','asc')->select();
            }
            Cache::set('admin_menu',$menu_list);
        }

        return $menu_list;
    }
    /**
     * 当前操作栏目
     * @param Request $request Request对象
     * @access public
     */
    public function now_menu($url_act)
    {
        $db = Db::name('admin_menu');
        //todo 暂时不创建模型
        $menu = $db->where('controller',$url_act['controller'])->find();
        
        return $menu['parent_id']>0?$menu['parent_id']:$menu['menu_id'];
    }
    /**
     * 权限校验
     * @param Request $param 参数
     * @access public
     */
    public function check_limits($param)
    {
        $where = [
            'app'=>$param['module'],
            'controller'=>$param['controller'],
            'action'=>$param['action'],
        ];
        //权限缓存todo
        if ($this->admin_user['admin_id']==1){
            return ['status'=>1];
        }
        //判断请求方法是否存在权限表
        $menu_id = model('AdminMenu')->where($where)->value('menu_id');
        //权限未录入权限表，则不进行权限控制
        if (!$menu_id){
            return ['status'=>1];
        }
        //用户所在权限组
        $w_group_id = model('AdminUserGroup')->where('w_admin_id',$this->admin_user['admin_id'])->column('w_group_id');
        if (!$w_group_id){
            return ['status'=>0];
        }
        //判断用户是否拥有此权限
        $map = [
            'w_menu_id'=>$menu_id,
            'w_group_id'=>array('in',$w_group_id),
        ];
        $run = model('AdminGroupMenu')->where($map)->find();
        if (!$run){
            return ['status'=>0];
        }
        return ['status'=>1];
    }
    /**
     * 创建操作日志
     * @param $content 日志内容
     * @access public
     */
    public function create_adminlog($content)
    {
        $log = model('AdminLog');
        // 模型对象赋值
        $log->data([
            'w_admin_id'  =>  $this->admin_user['admin_id'],
            'admin_name' =>  $this->admin_user['admin_name'],
            'content' =>  empty($content)?'':$content,
            'ctime' =>  time()
        ]);
        $log->save();
    }
    /**
     * 获取后台管理员ID
     * @access public
     */
    public function getAdminId()
    {
        return $this->admin_user['admin_id'];
    }
    /**
     * 获取配置信息
     * @access public
     */
    public function getConfig()
    {
        $config=Cache::get('config');
        if(empty($config)){
            $configs = Db::name('Config')->select();
            foreach ($configs as $key=>$value){
                $config[$value['key_name']]=$value['key_value'];
            }
            Cache::set('config',$config);
        }
        return $config;
    }

    /**
     * 清除缓存
     */
    public function clearCache(){
        Cache::rm('admin_menu');
        Cache::rm('config');
        header('location:'.url('/'));
    }

}
